package com.vnp.vchat;

import com.google.android.gcm.GCMRegistrar;
import com.vnp.vchat.UIControls.MyClickAbleImageView;
import com.vnp.vchat.Util.Common;
import com.vnp.vchat.Util.PreferenceHelper;
import com.vnp.vchat.Util.Utils;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.widget.ImageButton;
import android.widget.TextView;

public class SplashScreenActivity extends Activity implements OnTouchListener{

	PreferenceHelper preferenceHelper;
	TextView txtCopyRight, txtTilteDangnhap;
	ImageButton btnLoginVatgia, btnLoginFb;
	
	
	Handler mHandler = new Handler();
	Animation lefInAnimation, rightInAnimation, fadeIn;
	
	Runnable rMain = new Runnable() {
		
		@Override
		public void run() {
			gotoMain();
		}
	};
	
	Runnable r1, r2;
	
	Runnable rA = new Runnable() {
		
		@Override
		public void run() {
			showAnimation();
		}
	};
	
	Runnable rTitle = new Runnable() {
		
		@Override
		public void run() {
			txtTilteDangnhap.setVisibility(View.VISIBLE);
			txtTilteDangnhap.setAnimation(fadeIn);
		}
	};
	
	private BroadcastReceiver mReceiver;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash_screen);
		
		preferenceHelper = new PreferenceHelper(this);
		registerGCM();
		txtCopyRight = (TextView) findViewById(R.id.txt_copyright);
		txtTilteDangnhap = (TextView) findViewById(R.id.txt_title_dangnhap);
		
		btnLoginVatgia = (ImageButton) findViewById(R.id.btn_login_vatgia);
		btnLoginFb = (ImageButton) findViewById(R.id.btn_login_fb);
		
		lefInAnimation = AnimationUtils.loadAnimation(this, R.anim.pull_in_from_leftn);
		rightInAnimation = AnimationUtils.loadAnimation(this, R.anim.pull_in_from_rightn);
		fadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);
		setFont();
		
		
		// check xem da dang nhap chua roi moi cho hien thi 2 cai button nay
		if (!preferenceHelper.getUserId().equals("")){
			mHandler.postDelayed(rMain, 2000);
		} else {
//			mHandler.postDelayed(rTitle, 300);
			mHandler.postDelayed(rA, 300);
		}
		
//		mHandler.postDelayed(rMain, 2000);
		init();
		
		recivedBroadcast();
		
	}
	
	
	private void recivedBroadcast(){
		 IntentFilter intentFilter = new IntentFilter();
		 intentFilter.addAction("com.package.FINISH");
		    
		    mReceiver = new BroadcastReceiver() {
				
				@Override
				public void onReceive(Context context, Intent intent) {
					finish();
				}
			};
			
			// register
			this.registerReceiver(mReceiver, intentFilter);
	}
	
	
	private void init() {
		btnLoginFb.setOnTouchListener(this);
		btnLoginVatgia.setOnTouchListener(this);
	}
	
	private void setFont() {
		txtCopyRight.setTypeface(Utils.getTypefaceLight(SplashScreenActivity.this));
		txtTilteDangnhap.setTypeface(Utils.getTypefaceLight(SplashScreenActivity.this));
	}
	
	private void gotoMain() {
		Intent t = new Intent(SplashScreenActivity.this, MainActivity.class);
		finish();
		startActivity(t);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId()) {
		case R.id.btn_login_vatgia:
			if (event.getAction() == MotionEvent.ACTION_UP){
				gotoLoginVatgia();
			}
			
			break;
		case R.id.btn_login_fb:
			if (event.getAction() == MotionEvent.ACTION_UP){
				gotoLoginFb();
			}
			
			break;

		default:
			break;
		}
		return false;
	}
	
	public void creatRunableAnimation(){
		r1 = new Runnable() {
			
			@Override
			public void run() {
				btnLoginVatgia.setVisibility(View.VISIBLE);
				btnLoginVatgia.startAnimation(lefInAnimation);
			}
		};
		r2 = new Runnable() {
			
			@Override
			public void run() {
				btnLoginFb.setVisibility(View.VISIBLE);
				btnLoginFb.startAnimation(rightInAnimation);
			}
		};
	}
	
	void showAnimation(){
		creatRunableAnimation();
		mHandler.postDelayed(r1, 300);
		mHandler.postDelayed(r2, 300);
	}
	
	
	void gotoLoginVatgia(){
		Intent t = new Intent(SplashScreenActivity.this, LoginIdVatgiaActivity.class);
		startActivity(t);
	}
	
	void gotoLoginFb(){
		Intent t = new Intent(SplashScreenActivity.this, LoginWithFacebookActivity.class);
		finish();
		startActivity(t);
	}
	
	void registerGCM() {
		if (Build.FINGERPRINT.startsWith("generic")) {
			 // running on an emulator
				Log.e("EMULATOR", "======================");
			} else {
			// running on a device
			// Make sure the device has the proper dependencies.
				GCMRegistrar.checkDevice(SplashScreenActivity.this);
				
			// Make sure the manifest was properly set - comment out this line
			// while developing the app, then uncomment it when it's ready.	
				GCMRegistrar.checkManifest(SplashScreenActivity.this);
				
				String regId = GCMRegistrar.getRegistrationId(SplashScreenActivity.this);
				
				// Check if regid already presents
				if (regId.equals("")) {
					GCMRegistrar.register(this, Common.PROJECT_NUMBER);
				}
				
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						String regId="";
						
						while (regId.equals("")) {
							regId = GCMRegistrar.getRegistrationId(SplashScreenActivity.this);
							Log.i("vChat", "Registered with id "+regId);
							preferenceHelper.setDeviceId(regId);
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				}).start();
			}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mReceiver);
		mHandler.removeCallbacks(r1);
		mHandler.removeCallbacks(r2);
		mHandler.removeCallbacks(rA);
		mHandler.removeCallbacks(rMain);
		mHandler.removeCallbacksAndMessages(null);
//		System.exit(0);
//		mHandler.removeCallbacks(rTitle);
		Log.i("vChat", "ON destroy");
	}
	
	
	
}
